<?php
$form['#validate'][] = 'joomlart_engine_settings_validate';

//using some lib for admin
global $t3_base_theme_path, $t3_engine_path, $theme_key, $t3_theme_path;

$theme_key = $key;

$t3_engine_path = dirname($themes[$key]->owner);
$base_themes = array_keys($themes[$key]->base_themes);

$t3_base_theme_path = drupal_get_path('theme', $base_themes[0]);
$t3_theme_path = drupal_get_path('theme', $key);

require_once ($t3_engine_path.'/core/common.php');
require_once ($t3_engine_path.'/core/libs/object.php');
require_once ($t3_engine_path.'/core/theme-settings.php');

//for admin save setting, using ajax
$t3_task = $_REQUEST['t3_task'];
if($t3_task){
	require_once($t3_engine_path.'/core/admin.php');
	$t3_admin = new T3Admin($t3_task);
	$return = $t3_admin->execute($t3_task);
	exit;
}

if($key != 'core_jdt3'){
	function core_jdt3_settings($setting){
	  
		global $t3_base_theme_path, $t3_engine_path, $t3_theme_info, $theme_key;

		$theme = $theme_key;
		$t3_setting = new T3Setting($theme);

		//need to get all of profile properties, parse to json type and store to javascript variable
		$profiles = $t3_setting->getProfiles();


		$c_profile = $profiles['default'];

		$t3_js_string = drupal_to_js($profiles);
		$t3_js = 'var t3_old_profiles = '.$t3_js_string .";\n";
		$t3_js .= 'var t3_profiles = '.$t3_js_string .";\n";
		$t3_js .= 'var t3_current_theme = "'.$theme ."\";\n";

		$q = $_GET['q'];
		$t3_js .= 'var t3_q = "'.$q ."\";\n";

		drupal_add_js($t3_js, 'inline');
		drupal_add_js($t3_base_theme_path.'/js/admin/ui.core.js','theme');
		drupal_add_js($t3_base_theme_path.'/js/admin/ui.draggable.js','theme');
		drupal_add_js($t3_base_theme_path.'/js/admin/ui.tabs.js','theme');
		 
		//FOR TOOLTIP
		drupal_add_js($t3_base_theme_path.'/js/admin/jquery.tooltip.js','theme');
		 
		drupal_add_js($t3_base_theme_path.'/js/admin/ui.dialog.js','theme');
		drupal_add_js($t3_base_theme_path.'/js/admin/ui.sortable.js','theme');
		drupal_add_js($t3_base_theme_path.'/js/admin/fileuploader.js','theme');
		drupal_add_js($t3_base_theme_path.'/js/admin/ajaxupload.js','theme');
		drupal_add_js($t3_base_theme_path.'/js/admin/admin.js','theme');
		drupal_add_js($t3_base_theme_path.'/js/admin/help.js','theme');

		drupal_add_css($t3_base_theme_path.'/css/admin/ui.all.css');
		drupal_add_css($t3_base_theme_path.'/css/admin/admin.css');
		/*
		 * Create the form using Forms API: http://api.drupal.org/api/6
		 */
		$form = array();

		$theme_path = drupal_get_path('theme', $theme);
		if(!is_writeable($theme_path)){
			$html = '<div class="error">'.t('Folder "'.$theme_path.'" is unwriteable!"').'</div>';
			$form[] = array('#value'=>$html);
		}
		 
		$form['global_fieldset'] = array(
	    '#type' => 'fieldset',
	    '#title' => t('Global Settings'),
	    '#attributes'=>array('id'=>'jd-global-setting','class'=>'jd-global-setting'),
	    '#collapsible' => TRUE,
	    '#collapsed' => TRUE,
		);

		$html = '<div id="jd-theme-setting">
  			<ul class="tabs primary">
  				<ul class="tabs primary"> 
			        <li><a href="#theme-general">'.t('Global').'</a></li> 
			        <li><a href="#theme-profiles">'.t('Profiles').'</a></li> 
			        <li><a href="#theme-layouts">'.t('Layouts').'</a></li> 
			        <li><a href="#theme-themes">'.t('Themes').'</a></li> 
		    	</ul>';
		$html .= '<ul class="tabs secondary profile">';
		foreach ($profiles as $value=>$info){
			$class = $value == 'default'?'class ="jd-profile active"' : 'class="jd-profile"';
			$span = $value == 'default'?'<span class="jd-profile-action">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>' : '';
			$html .= '<li id="t3-admin-profile-'.$value.'"'.$class.'><a id="t3-admin-profile-a-'.$value.'" href="javascript:void(0)">'.$value.'</a>'.$span.'</li>';
		}
		 
		$html .= '<li class="jd-new-profile"><a href="javascript:void(0)">' .t('New') . '</a></span></li>';

		$html .= '<li class="ja-icon-help">
			<span>'.t('Help').'</span>
		  </li>';
		 
		$html .= '</ul>';
		$html .= '</ul>';

		//check t3 message and display
		$messages = t3_get_messages();
		if(count($messages)){

		}else{

		}
		$html .= '<div id="t3-message" style="display:none">&nbsp;</div>';
		 
		$form[] = array(
  		'#value'=>$html,
		);
		 
		//////////////////////////////////////////////////////////////////
		//////////////////////// GENERAL TAB /////////////////////////////
		//////////////////////////////////////////////////////////////////
		include('admin/general.php');
		//////////////////////////////////////////////////////////////////
		//////////////////////// PROFILE TAB /////////////////////////////
		//////////////////////////////////////////////////////////////////
		include('admin/profile.php');
		//////////////////////////////////////////////////////////////////
		////////////////// LAYOUT TAB ////////////////////////////////////
		//////////////////////////////////////////////////////////////////
		include('admin/layout.php');
		//////////////////////////////////////////////////////////////////
		////////////////// THEME TAB /////////////////////////////////////
		//////////////////////////////////////////////////////////////////
		include('admin/theme.php');
		 
		$form[] = array(
  		'#value'=>'</div></div>'
  		);
  		 
  		// Return the form
  		return $form;
	}
}
function joomlart_engine_settings_validate($form, &$form_state){

}